controller.controller('SlotIndexController',['$scope','slotService','clientService',function($scope,slotService,clientService){
    var slotColor=['','red','orange','yellow','olive','green','teal','blue','violet','purple','pink','brown','grey','black'];

    var clientInfoMapping={};

    var initSlots=function(){
        slotService.getSlots(function(response){
            var slots=response.data;

            if(objectUtil.isEmpty(slots)) {
                return;
            }

            for(var i=0;i<slots.length;i++) {
                var slot=slots[i];
                if(stringUtil.isBlank(slot.clientId)) {
                    slot.color=slotColor[0];
                }
                else {
                    slot.color=slotColor[Math.abs(stringUtil.hashCode(slot.clientId))%slotColor.length];
                }
            }

            $scope.slots=slots;
        });
    };

    var initClients=function(){
        clientService.getClients(function(response){
            var clients=response.data;

            if(objectUtil.isEmpty(clients)) {
                clientInfoMapping={};
                return;
            }

            for(var i=0;i<clients.length;i++) {
                var client=clients[i];
                clientInfoMapping[client.id]=client;
            }
        });
    };

    var getClientInfo=function(id){
        return clientInfoMapping[id];
    };

    initClients();
    initSlots();

    $scope.showClientInfo=function(id){
        var clientInfo=getClientInfo(id);
        if(objectUtil.isNull(clientInfo)) {
            core.ui.alert.warning('客户端不存在');
            return;
        }

        $scope.clientInfo=clientInfo;
        core.ui.dialog('#clientInfoDialog');
    };
}]);